package com.partsoft.dits.log;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Arrays;

public class BaseLogAttachment extends AbstractLogAttachment implements Cloneable, Serializable {
	
	private static final long serialVersionUID = -3618371960366809276L;
	
	private byte[] bytes = null;
	
	public BaseLogAttachment() {}
	
	public BaseLogAttachment(String name, byte[] bytes) {
		setName(name);
		setBytes(bytes);
	}
	
	public BaseLogAttachment(byte[] bytes) {
		setBytes(bytes);
	}

	public InputStream getInputStream() throws IOException {
		if (bytes == null) throw new IOException("无附件内容。");
		return new ByteArrayInputStream(bytes);
	}
	
	@Override
	public byte[] getBytes() throws IOException {
		return bytes;
	}
	
	public void setBytes(byte[] bytes) {
		this.bytes = bytes;
	}
	
	@Override
	public BaseLogAttachment clone() {
		BaseLogAttachment clone = (BaseLogAttachment) super.clone();
		if (clone != null) {
			clone.bytes = Arrays.copyOf(bytes, bytes.length);
		}
		return clone;
	}

}
